package com.ericsson.enap.modules.ebmLog.task.vgc;

import com.ericsson.common.quartz.task.ManualTask;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.enap.modules.ebmLog.entity.vgc.VgcEbmEventVO;
import com.ericsson.enap.modules.ebmLog.service.vgc.VgcEbmLogService;
import com.ericsson.enap.modules.sys.entity.EnipNetelement;
import com.ericsson.enap.modules.sys.entity.EpcDict;
import com.ericsson.enap.modules.sys.service.EpcDictService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.ericsson.enap.modules.ebmLog.constant.VgcEbmConstant.*;

/**
 * @description: 5gc ebm多维主任务
 * @author: wangjingshi
 * @create: 2021-11-09 10:02
 */
@Component
public class VgcEbmLogTask implements ManualTask {

    private final Logger log = LoggerUtils.Logger(LogFileName.VgcEbmLogJob);
    @Autowired
    private VgcEbmLogService vgcEbmLogService;
    @Autowired
    private EpcDictService epcDictService;


    @Override
    public void execute() {
        long startTime = new Date().getTime();
        log.info("===== do VgcEbmLogTask start =====: "+startTime);
        Integer delay = Integer.valueOf(epcDictService.selectEpcDict(new EpcDict(DELAY)).getKeyValue());
        log.info("===== delay: "+delay);
        String ebmPath = epcDictService.selectEpcDict(new EpcDict(EbmLogPath)).getKeyValue();
        log.info("===== ebmPath: "+ebmPath);
        String remotePath = epcDictService.selectEpcDict(new EpcDict(EbmLogDownloadPath)).getKeyValue();
        log.info("===== remotePath: "+remotePath);
        Integer ebmLogDays = Integer.valueOf(epcDictService.selectEpcDict(new EpcDict(EbmLogDays)).getKeyValue());
        log.info("===== ebmLogDays: "+ebmLogDays);
        /** 一、下载原始ebm文件,返回当前采集器采集的网元信息列表 */
        List<EnipNetelement> list = vgcEbmLogService.DownLoadEbmFiles(delay,ebmPath,remotePath);
        /** 二、执行士凯脚本解析指定字段，按事件输出到CSV文件中 */
        Map<String, VgcEbmEventVO> map = vgcEbmLogService.execScriptEbmFiles(list,ebmPath);
        /** 三、清除过期数据库表(mysql) */
        vgcEbmLogService.dropExpireTables(map,delay,ebmLogDays,list);
        /** 四、动态创建新表(mysql) */
        vgcEbmLogService.createTables(map,delay,list);
        /** 四、统计文件，最后把结果录入数据库 */
        vgcEbmLogService.statisticAndLoadDb(list,map,ebmPath,delay);
        long endTime = new Date().getTime();
        log.info("==== total time :"+(endTime-startTime)/1000/(double)60.00);
    }
}
